<?php

namespace Zilla\FrontBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Zilla\FrontBundle\Entity\ProWinker;

class WinkController extends Controller
{

    public function indexAction()
    {
        return $this->render('ZillaFrontBundle:Wink:index.html.twig');
    }

    public function pullNewsAction($slugName)
    {
        $result = array();
        $proWinker = $this->get('security.context')->getToken()->getUser();
        $winkedStores = $proWinker->getWinkedstores()->toArray();
        $test="not yet";
        $entityManager=$this->get('doctrine.orm.entity_manager');
        foreach($winkedStores as $winkedStore) {
            $time=$winkedStore->getLastModifiedDate()->getTimestamp();
            $time2=$winkedStore->getUpdatedAt()->getTimestamp();
            if ($winkedStore->getLastNotifiedDate()->getTimestamp() < $winkedStore->getLastModifiedDate()->getTimestamp()) {
                $winkedStore->setLastNotifiedDate($winkedStore->getLastModifiedDate());
                $entityManager->persist($winkedStore);
                $entityManager->flush();
                $result['data'][]=array('stores' => $winkedStore->getTitle());
               $test=$winkedStore->getTitle();
//               usleep(10000000);
            }
        }
        return new Response(json_encode($result));
    }

}
